unit EhLibZEOSDB;

interface

uses
  DbUtilsEh,DB,ZDataset, ZAbstractDataset,DBGridEh;

implementation

uses Classes;

//type
//  TADODataSetCr ack = class(TADODataSet);

function ADODataSetDriverName(DataSet: TZAbstractDataSet): String;
begin
  Result := 'MSSQL';
end;

function DateValueToADOSQLStringProc(DataSet: TDataSet; Value: Variant): String;
begin
  Result := DateValueToDataBaseSQLString(ADODataSetDriverName(TZQuery(DataSet)), Value)
end;

procedure SortDataInADODataSet(Grid: TCustomDBGridEh; DataSet: TZAbstractDataSet);
var
  s: String;
  i: Integer;
begin
  s := '';
  for i := 0 to Grid.SortMarkedColumns.Count - 1 do
  begin
    s := s + '[' + Grid.SortMarkedColumns[i].FieldName + ']';
    if Grid.SortMarkedColumns[i].Title.SortMarker = smUpEh
      then s := s + ' DESC, '
      else s := s + ', ';
  end;
  DataSet.SortedFields := Copy(s, 1, Length(s) - 2);
end;

type

  TZEOSSQLDatasetFeaturesEh = class(TSQLDatasetFeaturesEh)
  public
    procedure ApplySorting(Sender: TObject; DataSet: TDataSet; IsReopen: Boolean); override;
    procedure ApplyFilter(Sender: TObject; DataSet: TDataSet; IsReopen: Boolean); override;
    constructor Create; override;
  end;

//implementation

{ TADOSQLDatasetFeaturesEh }

procedure TZEOSSQLDatasetFeaturesEh.ApplyFilter(Sender: TObject;
  DataSet: TDataSet; IsReopen: Boolean);
begin
  inherited;
  if TDBGridEh(Sender).STFilter.Local then
  begin
    TDBGridEh(Sender).DataSource.DataSet.Filter :=
      GetExpressionAsFilterString(TDBGridEh(Sender),
        GetOneExpressionAsLocalFilterString, nil);
    TDBGridEh(Sender).DataSource.DataSet.Filtered := True;        
  end else
    ApplyFilterSQLBasedDataSet(TDBGridEh(Sender), DateValueToADOSQLStringProc, IsReopen, 'SQL');
end;

procedure TZEOSSQLDatasetFeaturesEh.ApplySorting(Sender: TObject;
  DataSet: TDataSet; IsReopen: Boolean);
begin
  if Sender is TCustomDBGridEh then
    if TCustomDBGridEh(Sender).SortLocal then
      SortDataInADODataSet(TCustomDBGridEh(Sender), TZAbstractDataSet(DataSet))
    else
      inherited ApplySorting(Sender, DataSet, IsReopen);
end;

constructor TZEOSSQLDatasetFeaturesEh.Create;
begin
  inherited Create;
  DateValueToSQLString := DateValueToADOSQLStringProc;
  SupportsLocalLike := True;
end;

initialization
  RegisterDatasetFeaturesEh(TZEOSSQLDatasetFeaturesEh, TZQuery);
end.
